package com.sunducation.wts.databinding2

import androidx.databinding.BaseObservable
import androidx.databinding.Bindable

/*BaseObservable和Observable的区别:

BaseObservable是实现了Observable的类, 帮我们实现了监听器的线程安全问题.
BaseObservable使用了PropertyChangeRegistry来执行OnPropertyChangedCallback
所以我不推荐你直接实现Observable.
*/
class ObservableUser : BaseObservable() {
    private var firstName: String? = null
    private var lastName: String? = null

    @Bindable
    fun getFirstName() : String?{
        return firstName;
    }

    // 注解才会自动在build目录BR类中生成entry, 要求方法名必须以get开头
    @Bindable
    fun getLastName():String? {
        return lastName;
    }

    fun setFirstName(firstName: String?) {
        this.firstName = firstName;
        notifyPropertyChanged(BR.firstName)
    }

    fun setLastName(lastName: String?) {
        this.lastName = lastName;
        notifyPropertyChanged(BR.lastName); // 需要手动刷新
    }
}